#if 0

#include <stdio.h>
#include <stdlib.h>

#define BUFFER_SIZE 10

typedef struct {
    int* buffer;      // 缓冲区数组指针
    int size;         // 缓冲区大小
    int head;         // 头部索引
    int tail;         // 尾部索引
} CircularBuffer;

// 创建环形缓冲区
CircularBuffer* createCircularBuffer(int size) {
    CircularBuffer* cb = (CircularBuffer*)malloc(sizeof(CircularBuffer));  // 分配内存空间
    cb->buffer = (int*)malloc(sizeof(int) * size);  // 分配缓冲区数据的内存空间
    cb->size = size;   // 设置缓冲区大小
    cb->head = 0;      // 初始化头部索引为0
    cb->tail = 0;      // 初始化尾部索引为0
    return cb;
}

// 销毁环形缓冲区
void destroyCircularBuffer(CircularBuffer* cb) {
    free(cb->buffer);  // 释放缓冲区数据的内存空间
    free(cb);          // 释放缓冲区结构体的内存空间
}

// 判断环形缓冲区是否已满
int isCircularBufferFull(CircularBuffer* cb) {
    return ((cb->tail + 1) % cb->size == cb->head);
}

// 判断环形缓冲区是否为空
int isCircularBufferEmpty(CircularBuffer* cb) {
    return (cb->head == cb->tail);
}

// 写入数据到环形缓冲区
void writeData(CircularBuffer* cb, int data) {
    if (isCircularBufferFull(cb)) {  // 如果缓冲区已满，则无法写入数据
        printf("Circular buffer is full. Data cannot be written.\n");
        return;
    }
    cb->buffer[cb->tail] = data;  // 将数据写入缓冲区的尾部
    cb->tail = (cb->tail + 1) % cb->size;  // 更新尾部索引，循环利用缓冲区空间
}

// 从环形缓冲区读取数据
int readData(CircularBuffer* cb) {
    if (isCircularBufferEmpty(cb)) {  // 如果缓冲区为空，则无数据可读取
        printf("Circular buffer is empty. No data to read.\n");
        return -1;  // 返回一个默认值表示读取失败
    }
    int data = cb->buffer[cb->head];  // 从缓冲区的头部读取数据
    cb->head = (cb->head + 1) % cb->size;  // 更新头部索引，循环利用缓冲区空间
    return data;
}

int main222() {
    CircularBuffer* cb = createCircularBuffer(BUFFER_SIZE);  // 创建大小为BUFFER_SIZE的环形缓冲区

    writeData(cb, 1);  // 写入数据1
    writeData(cb, 2);  // 写入数据2
    writeData(cb, 3);  // 写入数据3

    printf("Read data: %d\n", readData(cb));  // 读取数据并打印
    printf("Read data: %d\n", readData(cb));

    writeData(cb, 4);
    writeData(cb, 5);

    printf("Read data: %d\n", readData(cb));
    printf("Read data: %d\n", readData(cb));
    printf("Read data: %d\n", readData(cb));

    destroyCircularBuffer(cb);  // 销毁环形缓冲区

    return 0;
}

#elif 0
/**
* @brief  往环形缓冲写入数据
* @param  uint8_t data 待写入的数据 ;Type_RingBuffer_Uchar* pRingBuffer 要写入的缓冲区
* @note   往环形缓冲区写入uint8_t类型的数据
*/
void Ringbuf_wirte_Uchar(uint8_t data, Type_RingBuffer_Uchar* pRingBuffer)
{
 
    pRingBuffer->ringBuffer[pRingBuffer->tail] = data;
    if(++pRingBuffer->tail >= MAX_BUFFER) /*判断缓冲区是否已满*/
    {
        pRingBuffer->tail = 0;
    }
    if(pRingBuffer->tail == pRingBuffer->head)
    {
        if(++pRingBuffer->head >= MAX_BUFFER)/*防止越界*/
        {
            pRingBuffer->head = 0;
        }
    }
    pRingBuffer->length++;
}
 
/**
* @brief  从环形缓冲区读取数据
* @param  uint8_t* pdata  存放读取的数据的地址 ;Type_RingBuffer_Uchar* pRingBuffer 要读取的缓冲区
* @return 1:环形缓冲区为空，读取失败;0:读取成功
* @note   从环形缓冲区读取uint8_t类型的数据
*/
uint8_t Ringbuf_read_Uchar(uint8_t *pdata, Type_RingBuffer_Uchar* pRingBuffer)
{
    if(pRingBuffer->head == pRingBuffer->tail) /*判断缓冲区是否为空*/
    {
        return 1;
    }
    else
    {
        *pdata = pRingBuffer->ringBuffer[pRingBuffer->head];
        if(++pRingBuffer->head >= MAX_BUFFER) /*防止越界*/
        {
            pRingBuffer->head = 0;
        }
        pRingBuffer->length--;
        return 0;
    }
}



#endif


